package com.ddtech.business.bank.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddtech.annotation.AnonymousAccess;
import com.ddtech.business.bank.bo.AddBankQueAndOpt;
import com.ddtech.business.bank.entity.QuestionBank;
import com.ddtech.business.bank.query.QuestionBankQuery;
import com.ddtech.business.bank.service.QuestionBankQuestionOptionService;
import com.ddtech.business.bank.service.QuestionBankQuestionService;
import com.ddtech.business.bank.service.QuestionBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * 题库
 *
 * @author bin
 * @date 2023/2/11 0:14
 */
@Api(tags = "业务-题库")
@RestController
@RequestMapping("/question")
public class QuestionBankController {

    @Autowired
    QuestionBankService questionBankService;

    @Autowired
    QuestionBankQuestionService questionBankQuestionService;

    @Autowired
    QuestionBankQuestionOptionService questionBankQuestionOptionService;

    /**
     * 获取题库列表
     *
     * @param keyWords 关键字
     * @param page     页码
     * @param size     页面大小
     */
    @ApiOperation(value = "获取题库列表")
    @GetMapping("/bank-list")
    public ResponseEntity getQueBankList(@RequestParam(required = false) @ApiParam("关键字") String keyWords,
                                         @RequestParam(required = false) @ApiParam("题库类型") Integer type,
                                         @RequestParam int page,
                                         @RequestParam int size) {
        return new ResponseEntity(questionBankService.getQueBankList(keyWords, type, page, size), HttpStatus.OK);
    }

    /**
     * 获取题库列表
     *
     * @param questionBankQuery 关键字
     */
    @ApiOperation(value = "获取题库列表")
    @GetMapping("/bank-list-no-page")
    public ResponseEntity getQueBankListNoPage(QuestionBankQuery questionBankQuery) {
        return new ResponseEntity(questionBankService.getQueBankListNoPage(questionBankQuery), HttpStatus.OK);
    }

    /**
     * 获取题库列表
     *
     * @param questionBankQuery 关键字
     */
    @ApiOperation(value = "获取题库标签")
    @GetMapping("/bank-list-source")
    public ResponseEntity queBankListSource(QuestionBankQuery questionBankQuery) {
        return new ResponseEntity(questionBankService.getQueBankListSource(questionBankQuery), HttpStatus.OK);
    }

    /***
     * description: 查询题库汇总信息
     * @param:  * @param questionBankQuery
     * @date: 2023-04-28
     * @return: org.springframework.http.ResponseEntity
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @ApiOperation(value = "查询题库信息汇总")
    @GetMapping(value = "/bank-list-total")
    public ResponseEntity bankListTotal(QuestionBankQuery questionBankQuery) {
        return new ResponseEntity(questionBankService.bankListTotal(questionBankQuery), HttpStatus.OK);
    }

    /**
     * 获取题目列表
     *
     * @param bankId 题库id
     */
    @ApiOperation("获取题目列表")
    @GetMapping("/que-List")
    public ResponseEntity getQueList(@RequestParam @ApiParam("题库id") String bankId,
                                     String questionDesc,
                                     String optionType,
                                     @RequestParam(required = false, defaultValue = "0") int page,
                                     @RequestParam(required = false, defaultValue = "10") int size) {
        return new ResponseEntity(questionBankService.getListPageData(bankId, questionDesc, optionType, page, size), HttpStatus.OK);
    }

    /**
     * 添加或修改题库
     *
     * @param questionBank
     * @return
     */
    @PostMapping(value = "/addOrUpdateBank")
    @ApiOperation("添加或修改题库")
    public ResponseEntity saveOrUpdate(@Valid @RequestBody QuestionBank questionBank) {
        questionBankService.doSaveOrUpdate(questionBank);
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 添加或修改题库题目
     *
     * @param addBankQueAndOpt 题目实体
     * @return JsonResult
     * @throws Exception
     */
    @ApiOperation(value = "添加或修改题库题目")
    @PostMapping("/addOrUpdateQueAndOpt")
    public ResponseEntity addOrUpdateQueAndOpt(@RequestBody @ApiParam("题目实体") AddBankQueAndOpt addBankQueAndOpt) {
        questionBankService.addOrUpdateQueAndOpt(addBankQueAndOpt);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation("获取详情")
    @PostMapping(value = "/detail/{id}")
    public ResponseEntity info(@PathVariable String id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("questionBank", questionBankService.getById(id));
        map.put("queNum", questionBankQuestionService.questionBankQuestionList(id).size());
        return new ResponseEntity(map, HttpStatus.OK);
    }

    /**
     * 删除题库
     *
     * @param bankId 题库id
     * @return ApiResponse
     * @throws Exception
     */
    @ApiOperation(value = "删除题库包含题目及题目选项")
    @PostMapping("/deleteBank/{bankId}")
    public ResponseEntity deleteBank(@PathVariable("bankId") @ApiParam("题库id") String bankId) {
        questionBankService.deleteBank(bankId);
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * description:删除题目及题目选项
     *
     * @param: * @param id
     * @date: 2023-04-28
     * @return: org.springframework.http.ResponseEntity
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @ApiOperation(value = "删除题目及题目选项")
    @PostMapping("/deleteBankQuesAndOption/{id}")
    public ResponseEntity deleteBankQuesAndOption(@PathVariable("id") @ApiParam("题目id") String id) {
        questionBankService.deleteBankQuesAndOption(id);
        return new ResponseEntity(HttpStatus.OK);
    }


    /**
     * 删除题目选项
     *
     * @param id 选项id
     * @return ApiResponse
     * @throws Exception
     */
    @ApiOperation(value = "删除题目选项")
    @PostMapping("/deleteBankQuesOption/{id}")
    public ResponseEntity deleteBankQuesOption(@PathVariable("id") @ApiParam("选项id") String id) {
        questionBankQuestionOptionService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 根据题库id获取题目总数
     *
     * @param bankIds 题库ids
     * @return ApiResponse
     * @throws Exception
     */
    @ApiOperation(value = "根据题库id获取题目总数")
    @GetMapping("/getQueCount")
    public ResponseEntity getQueCount(@RequestParam @ApiParam("题库ids") String bankIds) {
        return new ResponseEntity(questionBankQuestionService.getQueCount(bankIds), HttpStatus.OK);
    }

    @ApiOperation(value = "下载题目导入模版")
    @AnonymousAccess
    @GetMapping("/downLoadQueTel")
    public void downLoadQueTel(HttpServletResponse response) {
        questionBankService.downLoadQueTel(response);
    }

    @ApiOperation(value = "导入题目")
    @PostMapping("/importQue")
    public ResponseEntity importQue(@RequestParam @ApiParam("题库id") String bankId,
                                    @RequestParam @ApiParam(value = "导入题目模板文件", required = true) MultipartFile file) {
        return new ResponseEntity(questionBankService.importQue(bankId, file), HttpStatus.OK);
    }

    @ApiOperation(value = "导入题目V2")
    @PostMapping("/importQueV2")
    public ResponseEntity importQueV2(@RequestParam @ApiParam("题库id") String bankId, String majorId,
                                      @RequestParam @ApiParam(value = "导入题目模板文件", required = true) MultipartFile file) {
        return new ResponseEntity(questionBankService.importQueV2(bankId,majorId, file), HttpStatus.OK);
    }



    @ApiOperation(value = "导出题目")
    @GetMapping("/exportQue")
    public void exportQue(@RequestParam @ApiParam("题库id") String bankId,
                          HttpServletResponse response) {
        questionBankService.exportQue(bankId, response);
    }

    @ApiOperation(value = "导出题目")
    @GetMapping("/exportQueV2")
    public void exportQueV2(@RequestParam @ApiParam("题库id") String bankId,
                          HttpServletResponse response) {
        questionBankService.exportQueV2(bankId, response);
    }

    @ApiOperation(value = "查询高频错题")
    @GetMapping("/many-error")
    public ResponseEntity<Object> getManyError(@RequestParam @ApiParam("页码") int page,
                                               @RequestParam @ApiParam("页面大小") int size,
                                               String questionName) {
        IPage pageableRequest = new Page(page+1, size);
        return new ResponseEntity(questionBankService.getManyError(pageableRequest,questionName), HttpStatus.OK);
    }
    @ApiOperation(value = "题库搜索")
    @GetMapping("/bankSearch")
    public ResponseEntity<Object> bankSearch(@RequestParam @ApiParam("页码") int page,
                                               @RequestParam @ApiParam("页面大小") int size,
                                               String questionName) {
        IPage pageableRequest = new Page(page+1, size);
        return new ResponseEntity(questionBankService.bankSearch(pageableRequest,questionName), HttpStatus.OK);
    }

}
